package david.java.tools;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 工具, 从DDL中抽取 字段
 * @Author: ZhaoDawei
 * @Date: Create in 8:21 下午 2022/3/28
 */
public class ColumnExtractor {

    public static void main(String[] args) throws IOException {
        String input = "src/tmp/src_column";
        String output_v = "target-tmp/tmp-vertical";
        String output_h = "target-tmp/tmp-horizontal";
        mkdirIfNotExists(output_h);
        mkdirIfNotExists(output_v);
        extract(input, output_h, true, false);
        extract(input, output_v, false, false);

    }

    private static void mkdirIfNotExists(String path) {
        File file = new File(path);
        String parent = file.getParent();
        if (parent.equals("/")) {
            return;
        }
        boolean exists = file.getParentFile().exists();
        if (!exists) {
            file.getParentFile().mkdirs();
        }
    }

    private static void extract(String input, String output) throws IOException {
        extract(input, output, false, true);
    }

    private static String backQuote(String str) {
        return "`" + str + "`";
    }

    private static void extract(String input, String output, boolean horizontal, boolean backQuote) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(input));
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        List<String> result = new ArrayList<>();

        String line;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isNotBlank(line)) {
                String[] split = line.split("\\s");
                inner:
                for (String str : split) {
                    // 过滤注释
                    if (str.startsWith("--")) {
                        break inner;
                    }
                    if (StringUtils.isNotBlank(str)) {
                        char[] chars = str.toCharArray();
                        StringBuilder sb = new StringBuilder();
                        for (char c : chars) {
                            if (c != '`' && c != ',') {
                                sb.append(c);
                            }
                        }
                        if(backQuote) {
                            result.add(backQuote(sb.toString()));
                        }else {
                            result.add(sb.toString());
                        }
                        break inner;
                    }
                }
            }
        }

        if (horizontal) {
            String join = String.join(", ", result);
            bw.write(join);
        } else {
            for (int i = 0; i < result.size(); i++) {
                if (i == 0) {
                    bw.write(result.get(i));
                } else {
                    bw.write(",");
                    bw.write(result.get(i));
                }
                bw.newLine();
            }
        }

        bw.flush();
        br.close();
        bw.close();
    }
}
